#!/usr/bin/env python3
import os
import re
import sys
import time
import subprocess
from datetime import datetime


current_time = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
tmplogfile = 'tpcc_output_{}.log'.format(current_time)

origincmd = ' '.join(sys.argv[1:])
tpcccmd = '{0} > {1}'.format(origincmd, tmplogfile)
print(tpcccmd)


monitorprecmd = 'cat {}'.format(tmplogfile)
monitorcmd = 'cat {} | tail -c 400'.format(tmplogfile)
monitorendcmd = 'cat {} | tail -n 7'.format(tmplogfile)


tpcc = subprocess.Popen(tpcccmd, stdout=subprocess.PIPE, shell=True)
time.sleep(1)
os.system(monitorprecmd)


i = 1
tpmtotal = 0
while tpcc.poll() is None:
    time.sleep(1)
    status, output = subprocess.getstatusoutput(monitorcmd)
    res = output.split('Term-00, ')[-1].replace('\b', '')
    
    parts = res.split()
    avgtmpTOTAL = float(parts[3])
    avgneworder = avgtmpTOTAL * 0.45

    print('{0}\t{1}\tResult:{2:.2f}'.format(i, res, avgneworder))
    i += 1


os.system(monitorendcmd)
